/*
 * claplan_beam.cc
 *
 *  Created on: 2 Nov 2013
 *      Author: Tom Vercauteren
 */

#include "claplan_beam.h"

#include "claplan_print.h"
#include "string_utilities.h"

using std::string;
using std::endl;

ClaplanBeam::ClaplanBeam() :
		type_bg(0), isocenter_anastruct(""), gantry(0), table(0), coll(0), linac(
				""), modality(ENERGY_MODALITIES_UNDEFINED), energy(0), wedge(
				""), accessory(""), contoured_anastruct(""), margin(0)
{
	mask.type_bg = 0;
	mask.isocenter_anastruct = 0;
	mask.gantry = 0;
	mask.table = 0;
	mask.coll = 0;
	mask.linac = 0;
	mask.modality = 0;
	mask.energy = 0;
	mask.wedge = 0;
	mask.accessory = 0;
	mask.contoured_anastruct = 0;
	mask.margin = 0;
}

ClaplanBeam::~ClaplanBeam()
{
}


ClaplanBeam::ClaplanBeam(const ClaplanBeam &cb)
{
	if (this != &cb) *this = cb;
}

ClaplanBeam& ClaplanBeam::operator=(const ClaplanBeam &cb)
{
	if(this != &cb)
	{
		mask = cb.mask;
		type_bg = cb.type_bg;
		isocenter_anastruct = cb.isocenter_anastruct;
		gantry = cb.gantry;
		table = cb.table;
		coll = cb.coll;
		linac = cb.linac;
		modality = cb.modality;
		energy = cb.energy;
		wedge = cb.wedge;
		accessory = cb.accessory;
		contoured_anastruct = cb.contoured_anastruct;
		margin = cb.margin;
	}
	return *this;
}

const std::string& ClaplanBeam::getAccessory() const
{
	return accessory;
}

void ClaplanBeam::setAccessory(const std::string& accessory)
{
	this->accessory = accessory;
	mask.accessory = 1;
}

void ClaplanBeam::unsetAccessory()
{
	this->accessory = "";
	mask.accessory = 0;
}

unsigned char ClaplanBeam::isAccessorySet() const
{
	return mask.accessory;
}

double ClaplanBeam::getColl() const
{
	return coll;
}

void ClaplanBeam::setColl(double coll)
{
	this->coll = coll;
	mask.coll = 1;
}

void ClaplanBeam::unsetColl()
{
	this->coll = 0;
	mask.coll = 0;
}

unsigned char ClaplanBeam::isCollSet() const
{
	return mask.coll;
}

const std::string& ClaplanBeam::getContouredAnastruct() const
{
	return contoured_anastruct;
}

void ClaplanBeam::setContouredAnastruct(const std::string& contouredAnastruct)
{
	contoured_anastruct = contouredAnastruct;
	mask.contoured_anastruct = 1;
}

void ClaplanBeam::unsetContouredAnastruct()
{
	contoured_anastruct = "";
	mask.contoured_anastruct = 0;
}

unsigned char ClaplanBeam::isContouredAnastructSet() const
{
	return mask.contoured_anastruct;
}

double ClaplanBeam::getEnergy() const
{
	return energy;
}

void ClaplanBeam::setEnergy(double energy)
{
	this->energy = energy;
	mask.energy = 1;
}

void ClaplanBeam::unsetEnergy()
{
	this->energy = 0;
	mask.energy = 0;
}

unsigned char ClaplanBeam::isEnergySet() const
{
	return mask.energy;
}

double ClaplanBeam::getGantry() const
{
	return gantry;
}

void ClaplanBeam::setGantry(double gantry)
{
	this->gantry = gantry;
	mask.gantry = 1;
}

void ClaplanBeam::unsetGantry()
{
	this->gantry = 0;
	mask.gantry = 0;
}

unsigned char ClaplanBeam::isGantrySet() const
{
	return mask.gantry;
}

const std::string& ClaplanBeam::getIsocenterAnastruct() const
{
	return isocenter_anastruct;
}

void ClaplanBeam::setIsocenterAnastruct(const std::string& isocenterAnastruct)
{
	isocenter_anastruct = isocenterAnastruct;
	mask.isocenter_anastruct = 1;
}

void ClaplanBeam::unsetIsocenterAnastruct()
{
	isocenter_anastruct = "";
	mask.isocenter_anastruct = 0;
}

unsigned char ClaplanBeam::isIsocenterAnastructSet() const
{
	return mask.isocenter_anastruct;
}

const std::string& ClaplanBeam::getLinac() const
{
	return linac;
}

void ClaplanBeam::setLinac(const std::string& linac)
{
	this->linac = linac;
	mask.linac = 1;
}

void ClaplanBeam::unsetLinac()
{
	this->linac = "";
	mask.linac = 0;
}

unsigned char ClaplanBeam::isLinacSet() const
{
	return mask.linac;
}

double ClaplanBeam::getMargin() const
{
	return margin;
}

void ClaplanBeam::setMargin(double margin)
{
	this->margin = margin;
	mask.margin = 1;
}

void ClaplanBeam::unsetMargin()
{
	this->margin = 0;
	mask.margin = 0;
}

unsigned char ClaplanBeam::isMarginSet() const
{
	return mask.margin;
}

EnergyModalities ClaplanBeam::getModality() const
{
	return modality;
}

void ClaplanBeam::setModality(EnergyModalities modality)
{
	this->modality = modality;
	mask.modality = 1;
}

void ClaplanBeam::unsetModality()
{
	this->modality = ENERGY_MODALITIES_UNDEFINED;
	mask.modality = 0;
}

unsigned char ClaplanBeam::isModalitySet() const
{
	return mask.modality;
}

double ClaplanBeam::getTable() const
{
	return table;
}

void ClaplanBeam::setTable(double table)
{
	this->table = table;
	mask.table = 1;
}

void ClaplanBeam::unsetTable()
{
	this->table = 0;
	mask.table = 0;
}

unsigned char ClaplanBeam::isTableSet() const
{
	return mask.table;
}

int ClaplanBeam::getTypeBg() const
{
	return type_bg;
}

void ClaplanBeam::setTypeBg(int typeBg)
{
	type_bg = typeBg;
	mask.type_bg = 1;
}

void ClaplanBeam::unsetTypeBg()
{
	type_bg = 0;
	mask.type_bg = 0;
}

unsigned char ClaplanBeam::isTypeBgSet() const
{
	return mask.type_bg;
}

const std::string& ClaplanBeam::getWedge() const
{
	return wedge;
}

void ClaplanBeam::setWedge(const std::string& wedge)
{
	this->wedge = wedge;
	mask.wedge = 1;
}

void ClaplanBeam::unsetWedge()
{
	this->wedge = "";
	mask.wedge = 0;
}

unsigned char ClaplanBeam::isWedgeSet() const
{
	return mask.wedge;
}

std::ostream & operator<<(std::ostream & os, const ClaplanBeam &beam)
{
	string s;
	os << "beam :" << endl;
	if (beam.isTypeBgSet() & 1)
		print_kv<string, int>(os, "type_bg", beam.type_bg);
	if (beam.isIsocenterAnastructSet() & 1)
		print_kv<string, string>(os, "isocenter_anastruct",
				beam.isocenter_anastruct);
	if (beam.isGantrySet() & 1)
		print_kv<string, double>(os, "gantry", beam.gantry);
	if (beam.isTableSet() & 1)
		print_kv<string, double>(os, "table", beam.table);
	if (beam.isCollSet() & 1) print_kv<string, double>(os, "coll", beam.coll);
	if (beam.isLinacSet() & 1)
		print_kv<string, string>(os, "linac", beam.linac);

	if (beam.isModalitySet() & 1)
	{
		if (beam.getModality() == ENERGY_MODALITIES_PHOTONS) s = "photons";
		else if (beam.getModality() == ENERGY_MODALITIES_ELECTRONS) s =
				"electrons";
		else s = "undefined";
		print_kv<string, string>(os, "modality", s);
	}

	if (beam.isEnergySet() & 1)
	{
		s = double2str(beam.energy) + " ";
		if (beam.modality == ENERGY_MODALITIES_PHOTONS) s += "MV";
		else if (beam.modality == ENERGY_MODALITIES_ELECTRONS) s += "MeV";
		else s += "undefined";
		print_kv<string, string>(os, "energy", s);
	}

	if (beam.isWedgeSet() & 1)
		print_kv<string, string>(os, "wedge", beam.wedge);
	if (beam.isAccessorySet() & 1)
		print_kv<string, string>(os, "accessory", beam.accessory);
	if (beam.isContouredAnastructSet() & 1)
		print_kv<string, string>(os, "contoured_anastruct",
				beam.contoured_anastruct);
	if (beam.isMarginSet() & 1)
		print_kv<string, double>(os, "margin", beam.margin);
	return os << endl;
}
